#include <QMessageBox>
#include "centralwidget.h"
#include <QElapsedTimer>

CentralWidget::CentralWidget(QTableView *parent) :
    QTableView(parent)
{
    resize(80,50);

    const int COLCOUNT = 23;
    const QStringList colNameList = QStringList()<<tr("评级")<<tr("代码")<<tr("名字")
                                                 <<tr("涨跌幅")<<tr("现价")<<tr("涨跌")
                                                 <<tr("今开")<<tr("昨收")<<tr("最高")
                                                 <<tr("最低")<<tr("买入价")<<tr("卖出价")
                                                 <<tr("总量(手)")<<tr("金额(万)")<<tr("市盈率")
                                                 <<tr("换手率")<<tr("MA5")<<tr("MA10")
                                                 <<tr("MA20")<<tr("MA30")<<tr("MA60")
                                                 <<tr("当前日期")<<tr("当前时间");


    m_tableItems.clear();
    m_pModel = new QStandardItemModel;
    m_pModel->setColumnCount(COLCOUNT);
    m_pModel->setHorizontalHeaderLabels(colNameList);
    setModel(m_pModel);

    int row = 5;
    initModel(row);

    setSelectionBehavior(QAbstractItemView::SelectRows);

    connect(this, SIGNAL(clicked(const QModelIndex& index)), this,
            SLOT(slotClicked(const QModelIndex& index)));
    connect(this, SIGNAL(doubleClicked(const QModelIndex& index)), this,
            SLOT(slotDoubleClicked(const QModelIndex& index)));
}

CentralWidget:: ~CentralWidget()
{

}

void CentralWidget::initModel(int row)
{
//    m_pModel->setRowCount(row);

}

int CentralWidget::getCurrentRow()
{
    QModelIndex index = currentIndex();
    return index.row();
}

inline int CentralWidget::getRowCount()
{
    return m_pModel->rowCount();
}

inline int CentralWidget::getColCount()
{
    return m_pModel->columnCount();
}

void CentralWidget::createTableItems(int num)
{
    int orgStockNum = m_tableItems.count();
    if(orgStockNum < num)
    {
        int count = num - orgStockNum;
        for(int row = 0; row < count; row++)
        {
            QList<QStandardItem*> itemList;
            for(int col = 0; col < getColCount(); col++)
            {
                QStandardItem *item = new QStandardItem;
                item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                itemList.append(item);
            }

            m_tableItems.insert(orgStockNum+row, itemList);
            m_pModel->insertRow(orgStockNum+row,itemList);

//            qDebug()<<"        *****createTableItems->m_tableItems:"<<m_tableItems.count();
//            qDebug()<<"        *****createTableItems->m_pModel:"<<m_pModel->rowCount();
        }
    }
    else if(orgStockNum > num)
    {
        int row = orgStockNum -1;
        int to = num - 1;
        for(; row > to; row--)
        {
            QList<QStandardItem*> itemList = m_tableItems.value(row);
            int colcount = getColCount();
            for(int col = 0; col < colcount; col++)
            {
                QStandardItem* item = itemList.at(col);
                delete item;
                item = 0;
            }

            itemList.clear();

            m_tableItems.remove(row);
            m_pModel->removeRow(row);
        }
    }

}

void CentralWidget::slotUpdateStockInfo(QHash<int, STOCKINFO> stockInfoHash)
{

#if BARE_STOCKS
    m_pModel->removeRows(0, getRowCount());

    int count = stockInfoHash.count();
    for(int row = 0; row < count; row++)
    {
        QList<QStandardItem* > itemList;
        STOCKINFO stockInfor = stockInfoHash.value(row);
        QColor color = stockDisplayColor(stockInfor);

        for(int index = 0; index < getColCount(); index++)
        {
            QStandardItem *item = new QStandardItem;
            item->setForeground(QBrush(color));
            itemList.append(item);
        }


        initData(stockInfor, itemList);

        m_pModel->insertRow(row,itemList);
    }
#else
    int dataCount = stockInfoHash.count();
    int viewCount = m_tableItems.count();

//    qDebug()<<"        *****dataCount:"<<dataCount;
//    qDebug()<<"        *****viewCount:"<<viewCount;

    if(dataCount != viewCount)
    {
        createTableItems(dataCount);
    }

    for(int row = 0; row < dataCount; row++)
    {
        QList<QStandardItem* > itemList = m_tableItems.value(row);
        STOCKINFO stockInfor = stockInfoHash.value(row);
        QColor fgColor = stockInfor.fgColor;
        QColor bgColor = stockInfor.bgColor;

        int colcount = getColCount();
        for(int col = 0; col < colcount; col++)
        {
            itemList.at(col)->setForeground(QBrush(fgColor));
            itemList.at(col)->setBackground(QBrush(bgColor));
        }

        initData(stockInfor, itemList);
    }
#endif

//    qDebug()<<"**************************************************\n\n";
}

QColor CentralWidget::stockDisplayColor(STOCKINFO &stockInfo)
{
    if(stockInfo.fDifference > 0)
    {
        return QColor("red");
    }
    else if(stockInfo.fDifference < 0)
    {
        return QColor("green");
    }

    return QColor("black");
}

void CentralWidget::initData(STOCKINFO &stockInfo, QList<QStandardItem* > &itemList)
{
    itemList.at(0)->setBackground(QBrush(QColor("yellow")));
    itemList.at(0)->setForeground(QBrush(QColor("red")));
    itemList.at(0)->setDragEnabled(true);
    itemList.at(0)->setDropEnabled(true);

    itemList.at(0)->setData(QVariant(stockInfo.sRecommend), Qt::DisplayRole);

    itemList.at(1)->setData(QVariant(stockInfo.sStockCode), Qt::DisplayRole);
    itemList.at(2)->setData(QVariant(stockInfo.sStockName), Qt::DisplayRole);

    QString str =  QString::number(stockInfo.fRangeExtent * 100);
    itemList.at(3)->setText(tr("%1%").arg(str));

    itemList.at(4)->setData(QVariant(stockInfo.fCurrentPrice), Qt::DisplayRole);
    itemList.at(5)->setData(QVariant(stockInfo.fDifference), Qt::DisplayRole);

    itemList.at(6)->setData(QVariant(stockInfo.fStartPrice), Qt::DisplayRole);
    itemList.at(7)->setData(QVariant(stockInfo.fLastEndPrice), Qt::DisplayRole);
    itemList.at(8)->setData(QVariant(stockInfo.fTopPrice), Qt::DisplayRole);
    itemList.at(9)->setData(QVariant(stockInfo.fFootPrice), Qt::DisplayRole);

    itemList.at(10)->setData(QVariant(stockInfo.fBuyPrice), Qt::DisplayRole);
    itemList.at(11)->setData(QVariant(stockInfo.fSalePrice), Qt::DisplayRole);
    itemList.at(12)->setData(QVariant(stockInfo.fStockSum), Qt::DisplayRole);
    itemList.at(13)->setData(QVariant(stockInfo.fTradeValue), Qt::DisplayRole);

    itemList.at(14)->setData(QVariant(stockInfo.fPERatio), Qt::DisplayRole);
    itemList.at(15)->setData(QVariant(stockInfo.fTurnoverRate), Qt::DisplayRole);

    itemList.at(16)->setData(QVariant(stockInfo.fMAFive), Qt::DisplayRole);
    itemList.at(17)->setData(QVariant(stockInfo.fMATen), Qt::DisplayRole);
    itemList.at(18)->setData(QVariant(stockInfo.fMATwenty), Qt::DisplayRole);
    itemList.at(19)->setData(QVariant(stockInfo.fMAThirty), Qt::DisplayRole);
    itemList.at(20)->setData(QVariant(stockInfo.fMASixty), Qt::DisplayRole);

    itemList.at(21)->setData(QVariant(stockInfo.dCurrentDate), Qt::DisplayRole);
    itemList.at(22)->setData(QVariant(stockInfo.tCurrentTime), Qt::DisplayRole);

}

void CentralWidget::slotClicked(const QModelIndex& index)
{

}

void CentralWidget::slotDoubleClicked(const QModelIndex& index)
{

}
